﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
public class Level8Memory : MonoBehaviour
{
    public GameLevelItem gameLevelItem;
    public List<TextButton> topTbList = new List<TextButton>();
    public List<TextButton> bottomTbList = new List<TextButton>();

    private List<string> topStrList = new List<string>();
    private List<string> bottomStrList = new List<string>();

    private TextButton tb1;
    private TextButton tb2;

    private bool isAni = false;
    private bool isStart = false;
    private int rightCount = 0;

    private float aniTime = 0.3f;
    // Start is called before the first frame update
    void Start()
    {
        topStrList.Add("l");
        topStrList.Add("o");
        topStrList.Add("v");
        topStrList.Add("e");
        bottomStrList.Add("l");
        bottomStrList.Add("o");
        bottomStrList.Add("v");
        bottomStrList.Add("e");
        InitTbList();
    }

    private void InitTbList()
    {
        int count = topTbList.Count;
        for (int i = 0; i < count; i++)
        {
            int tempIndex = i;
            topTbList[i].Init(GetRandomTopStr(), () =>
            {
                if (!isStart)
                    return;
                if (isAni)
                    return;
                if (null == tb1)
                {
                    tb1 = topTbList[tempIndex];
                    StartCoroutine(ShowTb1());
                }
                else if (null != tb1 && tb1 != topTbList[tempIndex])
                {
                    tb2 = topTbList[tempIndex];
                    StartCoroutine(ShowTb2());
                }
            });
        }

        count = bottomTbList.Count;
        for (int i = 0; i < count; i++)
        {
            int tempIndex = i;
            bottomTbList[i].Init(GetRandomBottomStr(), () =>
            {
                if (!isStart)
                    return;
                if (isAni)
                    return;
                if (null == tb1)
                {
                    tb1 = bottomTbList[tempIndex];
                    StartCoroutine(ShowTb1());
                }
                else if (null != tb1 && tb1 != bottomTbList[tempIndex])
                {
                    tb2 = bottomTbList[tempIndex];
                    StartCoroutine(ShowTb2());
                }
            });
        }

        StartCoroutine(StartLevel());
    }



    private string GetRandomBottomStr()
    {
        string randomStr = string.Empty;
        if (bottomStrList.Count > 0)
        {
            int randomIndex = Random.Range(0, bottomStrList.Count);
            randomStr = bottomStrList[randomIndex];
            bottomStrList.RemoveAt(randomIndex);
        }
        return randomStr;
    }

    private string GetRandomTopStr()
    {
        string randomStr = string.Empty;
        if (topStrList.Count > 0)
        {
            int randomIndex = Random.Range(0, topStrList.Count);
            randomStr = topStrList[randomIndex];
            topStrList.RemoveAt(randomIndex);
        }
        return randomStr;
    }

    private IEnumerator ShowTb1()
    {
        isAni = true;
        tb1.transform.DOScaleX(0, aniTime);
        yield return new WaitForSeconds(aniTime);
        tb1.text.DOFade(1, 0);
        tb1.transform.DOScaleX(1, aniTime);
        yield return new WaitForSeconds(aniTime);
        isAni = false;

    }

    private IEnumerator ShowTb2()
    {
        isAni = true;
        tb2.transform.DOScaleX(0, aniTime);
        yield return new WaitForSeconds(aniTime);
        tb2.text.DOFade(1, 0);
        tb2.transform.DOScaleX(1, aniTime);
        yield return new WaitForSeconds(aniTime);
        JugeResult();
    }

    private IEnumerator HideTb1()
    {
        tb1.transform.DOScaleX(0, aniTime);
        yield return new WaitForSeconds(aniTime);
        tb1.text.DOFade(0, 0);
        tb1.transform.DOScaleX(1, aniTime);
        yield return new WaitForSeconds(aniTime);
        tb1 = null;
        isAni = false;
    }

    private IEnumerator HideTb2()
    {
        tb2.transform.DOScaleX(0, aniTime);
        yield return new WaitForSeconds(aniTime);
        tb2.text.DOFade(0, 0);
        tb2.transform.DOScaleX(1, aniTime);
        yield return new WaitForSeconds(aniTime);
        tb2 = null;
        isAni = false;
    }

    private IEnumerator StartLevel()
    {
        yield return new WaitForSeconds(2.5f);
        // 缩小字母框
        int count = topTbList.Count;
        for (int i = 0; i < count; i++)
        {
            topTbList[i].transform.DOScaleX(0, aniTime);
        }
        count = bottomTbList.Count;
        for (int i = 0; i < count; i++)
        {
            bottomTbList[i].transform.DOScaleX(0, aniTime);
        }
        yield return new WaitForSeconds(aniTime);
        // 隐藏字母
        count = topTbList.Count;
        for (int i = 0; i < count; i++)
        {
            topTbList[i].text.DOFade(0, 0);
        }
        count = bottomTbList.Count;
        for (int i = 0; i < count; i++)
        {
            bottomTbList[i].text.DOFade(0, 0);
        }
        // 放大字母框
        count = topTbList.Count;
        for (int i = 0; i < count; i++)
        {
            topTbList[i].transform.DOScaleX(1, aniTime);
        }
        count = bottomTbList.Count;
        for (int i = 0; i < count; i++)
        {
            bottomTbList[i].transform.DOScaleX(1, aniTime);
        }
        yield return new WaitForSeconds(aniTime);
        isStart = true;
    }

    private void JugeResult()
    {
        if (tb1.text.text.Equals(tb2.text.text))
        {
            rightCount++;
            tb1 = null;
            tb2 = null;
            isAni = false;
            if (rightCount >= 4)
            {
                Invoke("TriggerResult", 0.5f);
            }
        }
        else
        {
            StartCoroutine(HideTb1());
            StartCoroutine(HideTb2());
        }
    }

    private void TriggerResult()
    {
        gameLevelItem.TriggerResult(true);
    }
}
